
/**
 * Created by 欧阳威 on 2016/7/28/2.16
 */
//准备开始数据库连接
var express = require('express');
var path = require('path');
var mongoose = require('mongoose');
var _ = require('underscore');
var Movie = require('./models/movie');
var bodyParser = require('body-parser');
var port = process.env.PORT || 3000;
var app = express();

var db = mongoose.connect('mongodb://127.0.0.1:27017/jcrop');
db.connection.on("error", function (error) {
    console.log("�数据库链接错误" + error);
});
db.connection.on("open", function () {
    console.log("�数据库链接成功�");
});
app.set('views', './views/pages');
app.set('view engine','jade');
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'bower_components')))
app.listen(port);

console.log('imooc stated on port' + port);

//index page
app.get('/', function(req, res){
    Movie.fetch(function(err,movies){
        if (err) {
            console.oog(err);
        }
        res.render('index', {
            title: '首页',
            movies: movies
            //   [
            //    {
            //    title:'机械战警',
            //    _id: 1,
            //    poster: 'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1'
            //},{
            //    title:'机械战警',
            //    _id: 2,
            //    poster: 'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1'
            //},{
            //    title:'机械战警',
            //    _id: 3,
            //    poster: 'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1'
            //},{
            //    title:'机械战警',
            //    _id: 4,
            //    poster: 'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1'
            //},{
            //    title:'机械战警',
            //    _id: 5,
            //    poster: 'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1'
            //},{
            //    title:'机械战警',
            //    _id: 6,
            //    poster: 'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1'
            //},
            //   ]
        })
    })


});

//detail page
app.get('/movie/:id', function(req, res){
    var id = req.params.id;

    Movie.findById(id, function(err, movie){
        res.render('detail', {
            title: 'imooc ' + movie.title,
            movie:movie
            //{
            //    doctor:"何塞@",
            //    country:"美国",
            //    title:'权力游戏',
            //    year:2016,
            //    poster:'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1',
            //    language:'英语',
            //    flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
            //    summary:"翻拍自2015年经典科幻片，翻拍自2015年经典科幻片，翻拍自2015年经典科幻片，翻拍自2015年经典科" +
            //    "幻片，翻拍自2015年经典科幻片，翻拍自2015年经典科幻片。"
            //}
        })
    })
});

//admin page
app.get('/admin/movie', function(req, res){
    res.render('admin', {
        title: 'imooc 后台录入页',
        movie :{
            title:'',
            doctor:'',
            country:'',
            year:'',
            poster:'',
            flash:'',
            summary:'',
            language:''
        }
    })
});

// admin updata movie
app.get('/admin/update/:id',function(req,res){
    var id = req.params.id;

    if(id){
        Movie.findById(id, function(err, movie){
            res.render('admin', {
                title: 'imooc 后台更新页',
                movie: movie
            })
        })
    }
})

// admin post movie
app.post('/admin/movie/new', function(res, req){
    var id = req.body.movie._id;
    var movieObj = req.body.movie;
    var _movie;

    if(id !== 'undefined'){ //说明查到了这不定影
        Movie.findById(id, function(err,movie) {
            if (err) {
                console.log(err)
            }

            _movie = _.extend(movie, movieObj);
            _movie.save(function(err, movie){
                if(err){
                    console.log(err)
                }

                res.redirect('/movie/' + movie._id)
            })
        })
    }
    else {
        _movie = new Movie({
            doctor: movieObj.doctor,
            title: movieObj.title,
            country: movieObj.country,
            language: movieObj.language,
            year: movieObj.year,
            poster: movieObj.poster,
            summary: movieObj.summary,
            flash: movieObj.flash
        })

        _movie.save(function(err, movie){
            if (err) {
                console.log(err);
            }

            res.redirect('/movie/' + movie._id)
        })
    }
})


//list page
app.get('/admin/list', function(req, res){
    Movie.fetch(function(err,movies){
        if (err) {
            console.oog(err);
        }

        res.render('list', {
            title: 'imooc 列表页',
            movies: movies
            //    [{
            //    title:'权力游戏',
            //    _id:1,
            //    doctor:'和晒@帕迪利亚',
            //    country:'美国',
            //    year:20156,
            // //   poster:'http://v.youku.com/v_show/id_XMTYzNDc2MTY3Mg==.html?from=y1.3-idx-beta-1519-23042.230771.2-1',
            //    language:'英语',
            //    flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
            //    summary:'翻拍自2015年经典科幻片,翻拍自2015年经典科幻片,翻拍自2015年经典科幻片,翻拍自2015年经典科幻片' +
            //    '翻拍自2015年经典科幻片翻拍自2015年经典科幻片翻拍自2015年经典科幻片翻拍自2015年经典科幻片。'
            //}]
        })
    })
});

//list detele movie
app.delete('admin/list',function(req,res){
    var id = req.query.id;

    if(id){
        movie.remove({_id:id}, function(err,movie){
            if(err){
                console.log(err)
            }
            else {
                res.json({success:1})
            }

        })
    }
})